package com.trkj.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.trkj.pojo.Emp;
import com.trkj.service.BackLogProcessService;
import com.trkj.service.WorkFlowService;
/**
 * 处理待办任务菜单的控制器
 * @author Lixd
 *
 */
@Controller
@SessionAttributes({"emp", "funcIds"})
public class BackLogProcessController {
	@Autowired
	WorkFlowService workFlowService;
	@Autowired
	BackLogProcessService backLogProcessService;
	/**
	 * @author Lixd
	 * @param useid
	 *            登录人id
	 * @param processDefinitionKey
	 * @return
	 */
	@RequestMapping("/find_my_task")
	@ResponseBody
	public Map<String, Object> findMyPersonalTask(Model model,String processID, String processType, String empName,
			Integer page, Integer rows) {
		Integer empid = getOnLineEmp(model);
		Map<String, Object> map = backLogProcessService.getPageBackLog(empid, processID, processType, empName, page,
				rows);
		if (map != null) {
			return map;
		}
		return null;
	}
	/**
	 * 获取登录人的id
	 * @author Lixd
	 * @param model
	 * @return
	 */
	private Integer getOnLineEmp(Model model) {
		Emp emp = (Emp)model.asMap().get("emp");
		Integer empid = emp.getEmpId();
		return empid;
	}
	/**
	 * 办理待办任务并保存批注
	 * @author Lixd
	 * @param taskId 从界面获取的当前任务id
	 * @param ishandle 从界面获取的审批结果
	 * @param comment 从界面获取的审批内容
	 * @param session 该会话的HttpSession对象
	 * @return
	 */
	@RequestMapping("/do_handle")
	@ResponseBody
	public boolean do_handle(Model model,String taskId , String ishandle,String comment) {
		String empid = getOnLineEmp(model).toString();
		return workFlowService.toCompleteMyPersonalTask(taskId, empid, ishandle, comment);
	}
	/**
	 * 通过流程id和流程名称查询流程并返回该流程到界面上
	 * @author Lixd
	 * @param processId 流程id 
	 * @param processName 流程名称，规定流程类别
	 * @return
	 */
	@RequestMapping("/findProcess")
	@ResponseBody
	public Object findProcess(Integer processId, String processName ) {
		Object handle = backLogProcessService.findProcess(processId, processName);
		if (handle!=null) {
			return handle;
		}
		return "流程不存在";
	}
}
